import sys
import os
import tkinter as tk

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from tkinter import filedialog
from tkinter import messagebox
from utils import ArgumentParser, ConfigLoader, LOG
from model import GLMModel, OpenAIModel
from translator import PDFTranslator

class UploadDownload():

    def __init__(self):
        self.file_path = None

    def translate_text(self, input_text):
        # 这里替换为你自己的翻译方法
        # 例如，你可以使用Google Translate API或其他翻译工具
        translated_text = f"这是翻译好的内容：\n\n{input_text}"
        return translated_text

    def open_file(self, file_info_label):
        upload_file_path = filedialog.askopenfilename(filetypes=[("文本文件", "*.txt"), ("PDF文件", "*.pdf"), ("Word文件", "*.doc *.docx")])
        if upload_file_path:
            file_info = self.get_file_info(upload_file_path)
            self.update_file_info_label(file_info, file_info_label)
            self.file_path = upload_file_path
            
    def get_file_info(self, upload_file_path):
        file_name = os.path.basename(upload_file_path)
        file_extension = os.path.splitext(file_name)[1]
        file_size = os.path.getsize(upload_file_path)
        return f"文件名: {file_name}\n扩展名: {file_extension}\n文件大小: {file_size} 字节"

    def translate_and_download(self):
        # 在这里进行翻译操作
        # 然后将翻译后的内容保存到一个新文件中
        argument_parser = ArgumentParser()
        args = argument_parser.parse_arguments()
        config_loader = ConfigLoader(args.config)

        config = config_loader.load_config()

        model_name = args.openai_model if args.openai_model else config['OpenAIModel']['model']
        api_key = args.openai_api_key if args.openai_api_key else config['OpenAIModel']['api_key']
        model = OpenAIModel(model=model_name, api_key=api_key)

        file_path = self.file_path
        file_format = args.file_format if args.file_format else config['common']['file_format']

        # 实例化 PDFTranslator 类，并调用 translate_pdf() 方法
        translator = PDFTranslator(model)
        translator.translate_pdf(file_path, file_format)

        messagebox.showinfo("翻译完成", "文件翻译完成并保存")

    def update_file_info_label(self, file_info, file_info_label):
        file_info_label.config(text=file_info)